BUI.use('bui/uploader', function(Uploader) {

	/**
	 * 返回数据的格式
	 * 
	 * 默认是 {url : 'url'},否则认为上传失败 可以通过isSuccess 更改判定成功失败的结构
	 */
	var uploader = new Uploader.Uploader(
			{
				render : '#walletdraw_uploader',
				url : '../../business/wallet/upload',
				text : '',
				name : 'file',
				rules : {
					// 文的类型
					ext : [ '.txt', '文件类型只能为{0}' ],
					// 上传的最大个数
					max : [ 1, '文件的最大个数不能超过{0}个' ],
					// 文件大小的最大值,单位也是kb
					maxSize : [ 1024, '文件大小不能大于1M' ]
				},
				// 根据业务需求来判断上传是否成功
				isSuccess : function(result) {
					if (result && result.status == '000') {
						if (result.data && result.data.code == 1) {
							BUI.Message.Alert("处理成功!");
						} else if (result.data && result.data.code == 2) {
							BUI.Message.Alert(result.data.msg + " 异常数据文件保存路径："
									+ result.data.filePath);
						} else {
							BUI.Message.Alert(result.data.msg);
						}
					} else {
						BUI.Message.Alert("上传失败!");
					}
				}
			}).render();
});

$("#btnSearch").click(function() {
	var serializeObj = {};
	$($("#searchForm").serializeArray()).each(function() {
		if(this.value != '') {
			serializeObj[this.name] = this.value;
		}
	});
	var searchParam = JSON.stringify(serializeObj);
	 $.ajax({
         type: "POST",
         url: "../../business/wallet/query",
         data: searchParam,
         dataType: "json",
         contentType:"application/json",
         success: function(data){
        	 if(data) {
        		 if(data.status == '000') {
        			 $('.walletDrawLogs tbody').empty();
        			 var dataArr = data.data;
        			 if(dataArr.length > 0) {
        				 var htmlArr = [];
        				 for(var i in dataArr){
        					 var item = dataArr[i];
        					 item.drawTime = new Date(item.drawTime).Format("yyyy-MM-dd hh:mm:ss");  
        					 item.status = formatStatus(item.status);
        					 item.amt = longToYJF(item.amt);
        					 
        					 htmlArr.push('<tr>');
        					 
        					 htmlArr.push('<td>');
        					 htmlArr.push('<input type="hidden" value="' + item.drawId + '"/>');
        					 htmlArr.push(item.userName);
        					 htmlArr.push('</td>');
        					 
        					 htmlArr.push('<td>');
        					 htmlArr.push(item.userRealName);
        					 htmlArr.push('</td>');
        					 
        					 htmlArr.push('<td>');
        					 htmlArr.push(item.amt);
        					 htmlArr.push('</td>');
        					 
        					 htmlArr.push('<td>');
        					 htmlArr.push(item.bankCardNo);
        					 htmlArr.push('</td>');
        					 
        					 htmlArr.push('<td>');
        					 htmlArr.push(item.drawTime);
        					 htmlArr.push('</td>');
        					 
        					 htmlArr.push('<td class="editStatus">');
        					 htmlArr.push(item.status);
        					 htmlArr.push('</td>');
        					 
        					 htmlArr.push('</tr>');
        					
        				 }
        				 
        				 $('.walletDrawLogs tbody').append(htmlArr.join(""));
        				 
        				 $('.editStatus').dblclick(function(event){
        					 if(_editstatus == 0)
        						 openEdit(event);
    					 });
        				 
        				 // 为当前点击的tr中所有除了.status的td绑定点击事件
        				 $('.editStatus').prevAll().click(function(event) {
        					if($(event.currentTarget).parent().children('.editStatus').find('select').length == 1)
        						updateStatus();
        				 });
        			 }
        		 } else {
        			 BUI.Message.Alert("查询失败!");
        		 }
        	 } else {
        		 BUI.Message.Alert("查询失败!");
        	 }
         }
    });
});

var _editstatus = 0;
var drawId;
var currentClickSatusText;
var currentClickStatusHtml;

function openEdit(event) {
	drawId = $(event.currentTarget).parent().children(':eq(0)').children('input').val();
	currentClickSatusText = event.currentTarget.innerText;
	currentClickStatusHtml = event.currentTarget.innerHTML;
	
	if(currentClickSatusText == '提现中') {
		$(event.currentTarget).empty().append(
				'<select name="status" style="width:100px;">' + 
		            '<option value="2" selected="selected">提现中</option>' +
		            '<option value="1">提现成功</option>' +
		            '<option value="0">提现失败</option>' +
	            '</select>'
	            );
	} else if(currentClickSatusText == '提现成功') {
		$(event.currentTarget).empty().append(
				'<select name="status" style="width:100px;">' + 
		            '<option value="2">提现中</option>' +
		            '<option value="1" selected="selected">提现成功</option>' +
		            '<option value="0">提现失败</option>' +
	            '</select>'
	            );
	} else {
		$(event.currentTarget).empty().append(
				'<select name="status" style="width:100px;">' + 
		            '<option value="2">提现中</option>' +
		            '<option value="1">提现成功</option>' +
		            '<option value="0" selected="selected">提现失败</option>' +
	            '</select>'
	            );
	}
	
	_editstatus = 1;
	
}


function updateStatus() {
	if(_editstatus == 0) 
		 return;
	 var selectTd = $('.editStatus select').parent();
	 
	 // 获取编辑下拉选择框中的value和text
	 var selectedVal = selectTd.children().val();
	 var selectedText = selectTd.find('option:selected').text();
	 
	if(selectedText == currentClickSatusText) { // 值和之前的一样，不发请求
		selectTd.empty().append(currentClickStatusHtml);
		_editstatus = 0;
		return;
	}
	
	// 不一样发请求更新数据
	var editParam = '{"drawId":' + drawId + ',"status":' + selectedVal + '}';
	$.ajax({
    type: "POST",
    url: "../../business/wallet/drawstatus/update",
    data: editParam,
    dataType: "json",
    contentType:"application/json",
    success: function(data){
	   	 if(data && data.status == '000') {
	   		alertMsg(1);
	   		selectTd.empty().append(formatStatus(selectedVal));
	   	 } else {
	   		alertMsg(0);
	   		selectTd.empty().append(currentClickStatusHtml);
	   	 }
    },
	 error: function() {
		 alertMsg(0);
		 selectTd.empty().append(currentClickStatusHtml);
	 }
  });
   
  _editstatus = 0;
}

function formatStatus(status) {
	if(status == 0) {
		return '<span style="color:#F12812;">提现失败</span>';
	} else if(status == 1) {
		return '<span style="color:#03A159;">提现成功</span>';
	} else {
		return '<span style="color:#0684CA;">提现中</span>';
	}
}

function alertMsg(status) {
	 var opratmsg;
	 var showicon;
	 if(status == 1) {
		 opratmsg = '操作成功!';
		 showicon = 'success';
	 } else {
		 opratmsg = '操作失败!';
		 showicon = 'error';
	 }
	 BUI.Message.Show({
	     msg : opratmsg,
	     icon : showicon,
	     buttons : [],
	     autoHide : true,
	     closeable: false
	   });
}